$(document).ready(function() {
    // Todo list
    $(".todo li").click(function() {
        $(this).toggleClass("todo-done");
    });

    // Make pagination demo work
    $(".pagination a").click(function() {
        if (!$(this).parent().hasClass("previous") && !$(this).parent().hasClass("next")) {
            $(this).parent().siblings("li").removeClass("active");
            $(this).parent().addClass("active");
        }
    });

    $(".btn-group a").click(function() {
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
    });

    // Disable link click not scroll top
    $("a[href='#']").click(function() {
        return false
    });

});
var menu = document.getElementById('menu');
var docked=true;
window.onscroll = function () {
            if (!docked && scrollTop() <= 0) {
              menu.className = '';
              docked = true;
            }else if (docked && scrollTop() >0) {
              menu.className = 'docked';
              docked = false;
            }
          };
function scrollTop() {
          return document.body.scrollTop || document.documentElement.scrollTop;
}
//无限下拉列表
$(function(){
    //$('.dropdown-toggle').dropdown('toggle');
    var hasmore = true;
    var moreLocked = false;//未锁定状态
    //添加无限下拉功能
    $(window).on("scroll",function(){
        // 然后判断窗口的滚动条是否接近页面底部，这里的20可以自定义
        if ($(document).scrollTop() + $(window).height() > $(document).height() - 20) {
            if(hasmore && !moreLocked){
                getMoreProduct();
            }
        }
    });
    function getMoreProduct () {
        // body...
        var lastid = $("#products .ruobiyitile :last").attr("productid");
        moreLocked = true;//锁定下拉滚动请求
        if(lastid){
            $.post('/index/more',{"lastid":lastid,"store":store,"category":category},function (response) {
                // body...
                if(response.status==0 && response.data.length>0){
                    var nli = '';
                    $.each(response.data,function(i,data) {
                        nli =nli + '<li class="span12 ruobiyitile" productid='+data.id+'>\
                                        <div class="tile productrow">\
                                            <div class="row">\
                                            <div class="product span8">\
                                                <h3 class="tile-title">'+data.title+'</h3>\
                                                <div class="description">'+data.description+'</div>\
                                                <a class="moredesc"><i class="icon-chevron-down"></i>查看更多</a>\
                                            </div>\
                                            <div class="gobuy span3">\
                                                <a href="'+data.clickurl+'"  target="_blank"><img  alt="" src="'+data.image+'"></a>\
                                                <a class="btn btn-primary btn-large btn-block" href="'+data.clickurl+'" target="_blank">官网购买连接</a>\
                                                <a href="http://service.weibo.com/share/share.php?url=http://www.ruobiyi.com/&appkey=3209255093&pic='+data.image+'&ralateUid=&language=zh_cn&searchPic=false" class="shareweibo btn btn-large btn-block btn-danger" target="_blank">分享到新浪微博</a>\
                                                <a href="http://share.v.t.qq.com/index.php?c=share&a=index&url=http://www.ruobiyi.com/&appkey=801284272&pic='+data.image+'" class="shareqtui btn btn-large btn-block btn-info" target="_blank">分享到腾讯微博</a>\
                                            </div>\
                                            </div>\
                                          </div>\
                                    </li>';
                    });
                    $("#products .ruobiyitile :last").after(nli);
                    moreLocked = false;//解锁下拉滚动请求
                }else{
                    //没有更多的内容可以显示了，处理边界情况
                    disableMoreProduct();
                }
            });
        }
    }

    function disableMoreProduct () {
        hasmore = false;
        $("#nonemore").show();
    }
    
    $('ul').on('click','.moredesc',function(e){
        var target = $(e.target);
        if(target.prop("tagName").toLowerCase() == 'i'){
            target = target.parent(); 
        }
        if(target.text() == '查看更多'){
            target.empty();
            target.append('<i class="icon-chevron-up"></i>向上收起');
            target.prev().css('max-height','100%');
        }else if(target.text() == '向上收起'){
            target.empty();
            target.append('<i class="icon-chevron-down"></i>查看更多');
            target.prev().css('max-height','400px');
        }
    });

    $('ul').on('click','.shareweibo',function(e){
        var share = e.target;
        var title = $(share).parent().prev().children("h3").text();
        var href = $(share).attr('href');
        $(share).attr('href',href+'&title='+title);
    });

    $('ul').on('click','.shareqtui',function(e){
        var share = e.target;
        var title = $(share).parent().prev().children("h3").text();
        var href = $(share).attr('href');
        $(share).attr('href',href+'&title='+title+'&line1='+title);
    });
});